package com.faxsun.profile.core.dao;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.common.persistence.EntityConfiguration;
import org.hibernate.ejb.QueryHints;
import org.springframework.stereotype.Repository;

import com.faxsun.profile.core.domain.FSCustomer;
import com.faxsun.profile.core.domain.FSThirdPartyCustomer;
import com.faxsun.profile.core.domain.FSThirdPartyCustomerImpl;


@SuppressWarnings("deprecation")
@Repository("blThirdPartyCustomerDao")
public class FSThirdPartyCustomerDaoImpl implements FSThirdPartyCustomerDao {
	
	private static final Log LOG = LogFactory.getLog(FSThirdPartyCustomerDaoImpl.class);
	
	@PersistenceContext(unitName = "blPU")
	protected EntityManager em;

	@Resource(name = "blEntityConfiguration")
	protected EntityConfiguration entityConfiguration;
	
	
	@Override
	public FSThirdPartyCustomer create3thCustomerByUid(String uid) {
		FSThirdPartyCustomer customer3th = (FSThirdPartyCustomer) entityConfiguration.createEntityInstance(FSThirdPartyCustomer.class.getName());
		customer3th.setUid(uid);
		return customer3th;
	}

	@Override
	public FSThirdPartyCustomer save(FSThirdPartyCustomer customer3th) {
		return em.merge(customer3th);
	}

	@Override
	public boolean check3thCustomerByUid(String uid) {
		Query query = em
				.createQuery("SELECT uid FROM com.faxsun.profile.core.domain.FSThirdPartyCustomer fsCustomer"
						+ " where fsCustomer.uid=:uid");
		query.setParameter("uid", uid);
		query.setHint(QueryHints.HINT_CACHEABLE, true);
		query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSThirdPartyCustomer");

		@SuppressWarnings("unchecked")
		List<String> results = query.getResultList();

		return results == null || results.isEmpty() ? false : true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FSCustomer> readCustomerBy3thUid(String uid) {
		Query query = em
				.createQuery("SELECT customer FROM com.faxsun.profile.core.domain.FSThirdPartyCustomer fsCustomer"
						+ " where fsCustomer.uid=:uid");
        query.setParameter("uid", uid);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSThirdPartyCustomer");
        return query.getResultList();  
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FSThirdPartyCustomer> readThirdPartyCustomerForCustomer(Long customerId) {
		Query query = em
				.createQuery("SELECT fsCustomer FROM com.faxsun.profile.core.domain.FSThirdPartyCustomer fsCustomer"
						+ " where fsCustomer.customer.id=:customerId");
	    query.setParameter("customerId", customerId);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSThirdPartyCustomer");
        return query.getResultList();  
	}

	@Override
	public FSThirdPartyCustomer readCustomer3thByUid(String uid) {
		 return em.find(FSThirdPartyCustomerImpl.class, uid);
	}
	
	@Override
	public FSThirdPartyCustomer findCustomer3thByCustomerAndChannel(Long customerId, String channelId){
		Query query = em
				.createQuery("SELECT fsCustomer FROM com.faxsun.profile.core.domain.FSThirdPartyCustomer fsCustomer"
						+ " where fsCustomer.customer.id=:customerId and fsCustomer.channelId=:channelId");
	    query.setParameter("customerId", customerId);
	    query.setParameter("channelId", channelId);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSThirdPartyCustomer");
        @SuppressWarnings("rawtypes")
		List list = query.getResultList();
        return (FSThirdPartyCustomer) (list.size()>0?list.get(0):null);  
		
	}

	@Override
	public FSCustomer findCustomer3thByUnionId(String unionId) {
		Query query = em
				.createQuery("SELECT customer FROM com.faxsun.profile.core.domain.FSThirdPartyCustomer fsCustomer"
						+ " where fsCustomer.unionId=:unionId");
        query.setParameter("unionId", unionId);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSThirdPartyCustomer");
        @SuppressWarnings("rawtypes")
		List list = query.getResultList();
        if(list.size()>1){
        	LOG.error("unionId="+unionId+" has more than one record in 3th account");
        }
        return (FSCustomer) (list.size()>0?list.get(0):null);  
	}

	@Override
	public void delete(FSThirdPartyCustomer customer3th) {
		em.remove(customer3th);
		em.flush();
	}

}
